/**
 * Created by tu_pei on 2018/3/24.
 */

// $('.js_posAdd').on('click',function(){
//     var _upData={
//         subclinicId:$('input[name=subclinicId]').val()
//     }
//     $.get(baseUrl.base+baseUrl.auth+'selectDesktopInfo',_upData,function(res){
//         if(res.code == '0'){
//             alert("111");
//             baseFn.infoDialog(_txt)
//         }
//     },'json')
// })
var DocData={
    subclinicId:SessionData.subclinicId,
    initBackAccount:function(data){
        var that=this,_userData=data
        $('.backbalancedialog .js_userName').text(_userData.applyUserName);
        $('.backbalancedialog .js_userPhone').text(_userData.applyUserPhone);
        $('.backbalancedialog .js_subclinicName').text(_userData.subclinicName);
        $('.backbalancedialog .js_backAccount').text(_userData.backAccount+'元');
        $('.backbalancedialog .js_applyReason').text(_userData.applyReason);
        $('.backbalancedialog .js_applyReason').val(baseFn.notNull(_userData.applyReason)?_userData.applyReason:'无')
        if(_userData.isBack == 0){
            $('.backbalancedialog .js_isBack').text('审核中')
        }else{
            $('.backbalancedialog .js_isBack').text('完成退款')
        }        
    },
    backcardId:''//退款id
}
$(function(){
    // $('.Baby_Healthy_home_right_usually_container>li').on("mouseover",function(){
    //     $(this).addClass("activeHeight").siblings().removeClass("activeHeight");
    //     $(this).find("a").addClass("activeClass").parent().siblings().find("a").removeClass("activeClass");
    // })
    var _datas = {}
    if(SessionData.powerSub){
        _datas.subclinicId=SessionData.powerSub
    }
    $.ajax({
        url: baseUrl.base+"selectDesktopInfo",
        type: 'get',
        data: _datas,
        success: function (data) {
            if (data.code == "0") {
                var res = data.results;
                $('.orderCost').text(res.orderCost);
                $('.accountCost').text(res.accountCost);
                $('.cardCost').text(res.cardCost);
                $('.goodsNumber').text(res.goodsNumber);
                $('.arriveNumber').text(res.arriveNumber);
                $('.bookingNumber').text(res.bookingNumber);
                $('.unBookingNumber').text(res.unBookingNumber);
                $('.orderNumber').text(res.orderNumber);
                $('.userNumber').text(res.userNumber);
                $('.toDayOrderNumber').text(res.toDayOrderNumber);
                $('.toDayBackOrderNumber').text(res.toDayBackOrderNumber);
            }
            if($('.js_backAccountbtn').siblings('.toDayBackOrderNumber').text() != '0'){
                $('.js_backAccountbtn').on('click',function(){
                    backAccount();
                    DocData.backcardId=''
                })
                
            }
        }
    })
});
var backAccountDT=''
var backData={}
if(SessionData.powerSub){
    backData.subclinicId=SessionData.powerSub
}
function renderBackAccountDT(){
    if(backAccountDT == ''){
        backAccountDT = baseFn.rpDataTables('.js_backlist',{
            ajUrl:baseUrl.base+baseUrl.user+'selectBackCardInfoList',
            ajDataSrc:"results",
            ajData:backData,
            select:true,
            ajType:'get',
            scrollY:200,
            columns:[
                { "data": "subclinicName",defaultContent:'无'},
                
                { "data": "applyUserName",defaultContent:'无',className:'td2p',render:function(data,type,row, meta){
                    return '<p>'+data+'</p><p>'+(baseFn.notNull(row.applyUserPhone)?row.applyUserPhone:'无')+'</p>'
                }},
                { "data": "backAccount",defaultContent:'',className:'',render:function(data,type,row, meta){
                    return '￥' +  data
                }},
                { "data": "backcardId",width:'10%',defaultContent:'无',className:"td2p",render:function(data,type,row, meta){
                    return '<p>'+(row.isBack==1?'完成退款':'审核中')+'</p><a class="datatablebtn detail" href="javascript:void(0)" data-vals="'+row.backcardId+'">详情</a>';
        
                }}
            ],
            initComplete:function(){},
            drawCallback: function( settings ) {
                $('.detail').off('click').on('click',function(){
                    DocData.backcardId = $(this).data('vals')
                    var _tr = $(this).parents('tr');
                    var _data=backAccountDT.row(_tr).data();
                    DocData.initBackAccount(_data);
                    $('.dialogelm.backbalancedialog .zctablewrap').addClass('hidden')
                    $('.dialogelm.backbalancedialog .backdetail').removeClass('hidden') 
                })
            },
            selecd:function(res){
                var _dts= backAccountDT.row(res).data().backcardId
                if(_dts.isBack == 0){
                    DocData.backcardId =_dts.backcardId
                }
            }
        })
    }else{
        backAccountDT.ajax.reload()
    }
}
renderBackAccountDT()
$('.js_gobackbtn').on('click',function(){
    DocData.backcardId = ''
    $('.dialogelm.backbalancedialog .zctablewrap').removeClass('hidden')
    $('.dialogelm.backbalancedialog .backdetail').addClass('hidden') 
})
function backAccount(){
    renderBackAccountDT()
    $('.dialogelm.backbalancedialog .zctablewrap').removeClass('hidden')
    $('.dialogelm.backbalancedialog .backdetail').addClass('hidden')
    var _d = baseFn.dialog({
        title:'审核退款',
        content:$('.dialogelm.backbalancedialog'),
        width:'40em',
        height:'60vh',
        okValue:'同意退款',
        ok:function(){
            //权限判断 7 工作台 退款申请审批
            if(baseFn.getPower(7)){
                baseFn.infoDialog('您没有审批退款的权限！')
            }else{
                if(baseFn.notNull(DocData.backcardId)){
                    baseFn.dialog({
                        title:'确定退款',
                        content:'您确定同意该退款申请么？',
                        ok:function(){
                            baseFn.$post({
                                url:baseUrl.user+'updateBackCardInfo',
                                data:{
                                    backcardId:DocData.backcardId
                                },
                                calFn:function(){
                                    _d.close()
                                    baseFn.infoDialog('退款成功！')
                                    window.location.reload()
                                },
                                canNull:true
                            })
                        },
                        cancel:function(){}
                    })
                }else{
                    baseFn.infoDialog('请进入退款详情审核退款内容！')
                    return false;
                }
            }
            return false
        },
        cancel:function(){
            DocData.backcardId=''
        }
    })
}
baseFn.powerFn()